package com.tlzh.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpHeaders;
import org.springframework.http.client.reactive.ReactorClientHttpConnector;
import org.springframework.web.reactive.function.client.WebClient;

import reactor.core.Disposable;
import reactor.core.publisher.Flux;
import reactor.core.scheduler.Schedulers;
import reactor.netty.http.client.HttpClient;

import java.time.Duration;

@Configuration
public class WebClientConfig {

    @Value("${spring.ai.ollama.base-url}")
    private String baseUrl;

    @Value("${spring.ai.ollama.pyUrl}")
    private String pyUrl;

    @Bean
    public WebClient webClient() {

//        ExchangeStrategies strategies = ExchangeStrategies.builder()
//                .codecs(configurer -> {
//                    // 添加支持 application/x-ndjson 的解码器
//                    Jackson2JsonDecoder decoder = new Jackson2JsonDecoder();
//                    decoder.getObjectMapper().findAndRegisterModules();
//                    configurer.defaultCodecs().jackson2JsonDecoder(decoder);
//                    configurer.defaultCodecs().jackson2JsonEncoder(new Jackson2JsonEncoder());
//                    // 为 application/x-ndjson 注册媒体类型支持
//                    configurer.customCodecs().registerWithDefaultConfig((Object) decoder, (Consumer<CodecConfigurer.DefaultCodecConfig>) MediaType.valueOf("application/x-ndjson"));
//                    configurer.defaultCodecs().maxInMemorySize(16 * 1024 * 1024); // 可根据需要调整内存大小
//                })
//                .build();

        HttpClient httpClient = HttpClient.create()
                .responseTimeout(Duration.ofSeconds(1)); // 设置响应超时时间
        ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);

        return WebClient.builder()
                .baseUrl(baseUrl) // 设置基础URL
                .defaultHeader(HttpHeaders.ACCEPT_CHARSET, "UTF-8")
//                .exchangeStrategies(strategies)
                .build();
    }
    @Bean
    public WebClient webClient1() {

        HttpClient httpClient = HttpClient.create()
                .responseTimeout(Duration.ofSeconds(1)); // 设置响应超时时间
        ReactorClientHttpConnector connector = new ReactorClientHttpConnector(httpClient);

        return WebClient.builder()
                .baseUrl(pyUrl) // 设置基础URL
                .defaultHeader(HttpHeaders.ACCEPT_CHARSET, "UTF-8")
                .build();
    }
}
